package com.firmboy.threadPool;

import org.junit.Test;
import org.springframework.scheduling.concurrent.DefaultManagedAwareThreadFactory;

import java.util.concurrent.*;

/**
 * 测试线程池
 *
 * @author firmboy
 * @create 2018-04-14 下午12:21
 **/
public class PoolTest {


    @Test
    public void poolUse(){
        //线程池的应用
        //开启线程之后，怎样保证线程都执行完了再执行主线程
        ThreadPoolExecutor pool = PoolFactory.getPool();

        ThreadFactory factory = pool.getThreadFactory();


        


    }

    @Test
    public void pool(){

        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().getName() + " run");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };

        //线程池怎样创建   开完线程之后，怎样和主线程进行数据交换
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 20, 6L, TimeUnit.SECONDS, new ArrayBlockingQueue(5), new DefaultManagedAwareThreadFactory());
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);
        pool.execute(myRunnable);

        System.out.println("---先开六个---");
        System.out.println("核心线程数" + pool.getCorePoolSize());
        System.out.println("线程池数" + pool.getPoolSize());
        System.out.println("队列任务数" + pool.getQueue().size());

    }


}
